/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2011-2020, All rights reserved.
 */

package com.lopo.xss;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * xss过滤器.
 */
@Component
@ConditionalOnProperty(name = "com.lopo.xss.enabled", havingValue = "true")
@WebFilter(urlPatterns = {"/*"}, filterName = "xssFilter")
@Slf4j
public class XssFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

        if (request instanceof HttpServletRequest) {
            log.info("XssFilter doFilter");
            HttpServletRequest req = (HttpServletRequest) request;
            ServletRequest requestWrapper = new XssHttpServletRequestWrapper(req);
            chain.doFilter(requestWrapper, response);
            return;
        }
        chain.doFilter(request, response);
    }
}
